package cn.ljy.authority.web.controller.backstage.authority;


import cn.ljy.authority.model.SysDepartment;
import cn.ljy.authority.model.query.SysDepartmentQuery;
import cn.ljy.authority.service.SysDepartmentService;
import cn.ljy.authority.web.controller.backstage.BaseBackstageController;
import cn.ljy.common.model.result.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "后台部门模块")
@RestController
@RequestMapping(value = "/api/backstage/authority/department")
public class SysDepartmentController extends BaseBackstageController<SysDepartment, SysDepartmentQuery,Long> {

    @Autowired
    private SysDepartmentService baseService;

    @ApiOperation(value = "添加部门")
    @PreAuthorize("hasAuthority('sys:department:add')")
    @Override
    protected ResponseData add(@Valid @RequestBody SysDepartment sysDepartment) {
        return super.add(sysDepartment);
    }

    @ApiOperation(value = "读取部门指定信息")
    @PreAuthorize("hasAuthority('sys:department:query')")
    @Override
    protected ResponseData get(@PathVariable Long id) {
        return super.get(id);
    }


    @ApiOperation(value = "编辑部门")
    @PreAuthorize("hasAuthority('sys:department:edit')")
    @Override
    public ResponseData edit(@Valid @RequestBody SysDepartment sysDepartment){
        return super.edit(sysDepartment);
    }

    @ApiOperation(value = "批量删除指定的多个部门")
    @PreAuthorize("hasAuthority('sys:department:del')")
    @Override
    public ResponseData deletes(Long[] ids){
        return super.deletes(ids);
    }

    //=========================扩展部分=============================================//

    @ApiOperation(value = "部门的树形列表")
    @GetMapping("/tree")
    @PreAuthorize("hasAuthority('sys:department:query')")
    public ResponseData tree(SysDepartmentQuery sysDepartmentQuery){
        List<SysDepartment> sysDepartments = baseService.findTree(sysDepartmentQuery);
        return ResponseData.success(sysDepartments);
    }
}
